#! /bin/sh

if [ "$1" ] ; then
  cat <<EOF
Usage: getsysinfo
Collect some system data that are useful for debugging
hardware detection bugs.
EOF
  exit 0
fi

# collect some system data

dir=`mktemp -d /tmp/getsysinfo.XXXXXXXXXX`

[ -d "$dir" ] || exit 1

host=`hostname`
[ "$host" ] || host=xxx

mkdir -p "$dir/$host"

for i in \
  /proc/asound \
  /proc/bus/input \
  /proc/cpuinfo \
  /proc/device-tree \
  /proc/devices \
  /proc/dma \
  /proc/driver/nvram \
  /proc/fb \
  /proc/filesystems \
  /proc/iSeries \
  /proc/ide \
  /proc/interrupts \
  /proc/iomem \
  /proc/ioports \
  /proc/meminfo \
  /proc/modules \
  /proc/net/dev \
  /proc/partitions \
  /proc/scsi \
  /proc/sys/dev/cdrom/info \
  /proc/sys/dev/parport \
  /proc/tty \
  /proc/version \
  /sys \
  /var/log/boot.msg \
  /var/lib/hardware/udi
do
  if [ -e "$i" ] ; then
    echo "$i"
    cp -x -a --parents "$i" "$dir/$host" 2>/dev/null
    chmod -R u+w,a+r,a+X "$dir/$host"
    for i in `( cd "$dir/$host" ; find proc -type f -size 0 )` ; do
      cat "/$i" >"$dir/$host/$i"
    done
  fi
done

echo /proc/mounts
perl -nl -e 'print unless (split)[0] =~ /none|automount|:/' /proc/mounts >"$dir/$host/proc/mounts"

mkdir -p "$dir/$host/var/log"
echo "
------  dmesg start  ------
" >>"$dir/$host/var/log/boot.msg"
dmesg >>"$dir/$host/var/log/boot.msg"

if [ -x /usr/bin/lshal ] ; then
  echo lshal
  lshal >$dir/$host/lshal.txt 2>/dev/null
fi

file="$host.tar.gz"
tar -C "$dir" -zcf "$dir/$file" "$host"

rm -f "/tmp/$file"

if [ -e "/tmp/$file" ] ; then
  echo "Warning: /tmp/$file exists, no info written"\!
  rm -rf "$dir"
  exit 1
fi

ln -nf "$dir/$file" "/tmp/$file"

rm -rf "$dir"

echo "
System data written to: /tmp/$file"
